package cn.lanqiao.bigDate.api.controller;

import cn.lanqiao.bigDate.reslut.Result;
import cn.lanqiao.bigDate.service.data.DataFaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @author Marisa
 * @description 描述
 * @date 2022/4/5
 */
@Api(value = "class")
@RestController
@RequestMapping(DataFaceController.BASE_URL)
public class DataFaceController{
    public static final String BASE_URL = "dataFace";

    @Resource
    public DataFaceService dataFaceService;

    /**
     * 前往首页
     * @return  mav结果
     */
    @ApiOperation(value = "前往首页",notes = "返回一个ModelAndView，前往首页")
//    @RequestMapping(value = "toIndex")
    @GetMapping(value = "toIndex")
    public ModelAndView toIndex() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName(BASE_URL + "/index");
        return mav;
    }

    /**
     * 人脸登录页面
     * @return  mav
     */
    @ApiOperation(value = "人脸登录接口",notes = "返回一个mav，前往登录页面")
//    @RequestMapping(value = "faceLoginPage")
    @PostMapping(value = "faceLoginPage")
    public ModelAndView faceLoginPage() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName(BASE_URL + "/faceLogin");
        return mav;
    }

    /**
     * 人脸注册
     * @param descImg   人脸数据
     * @param userName  用户账号
     * @param password  密码
     * @return          是否注册成功
     */
    @ApiOperation(value = "人脸绑定",notes = "将账号绑定到人脸，实现人脸登录的功能")
    @PostMapping("faceCreate")
    public Result<String> faceCreate(String descImg, String userName, String password){
        Boolean isCreate = dataFaceService.create(descImg, userName, password);
        Result<String> result = new Result<>();
        result.setCode(isCreate ? 200 : 500);
        result.setMsg( isCreate ?"create success!" : "create fail!");
        result.setData(null);
        return result;
    }

    /**
     * 人脸登录
     * @param descImg   人俩数据
     * @return          结果
     */
    @ApiOperation(value = "人脸登录接口",notes = "使用人脸进行账号登录，避免了输入操作，提高了系统安全性")
//    @RequestMapping("faceLogin")
    @PostMapping("faceLogin")
    public Result<Boolean> faceLogin(String descImg){
        Boolean isPass = dataFaceService.login(descImg);
        Result<Boolean> result = new Result<>();
        result.setCode(isPass ? 200 : 500);
        result.setMsg( isPass ?"login success!" : "login fail!");
        result.setData(isPass);
        return result;
    }

    @ApiOperation(value = "test interface", notes = "用于接口数据测试")
    @RequestMapping("test")
    public Result<Boolean> test(){
        Result<Boolean> result = new Result<>();
        result.setCode(200);
        result.setMsg("访问成功");
        result.setData(false);
        return result;
    }
}
